package com.ce.cetag.tags.menu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;


/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：菜单条标签
*
* @author kuanglingxuan
* Created on 2007-8-29
*/
public class MenuBarTag extends BodyTagSupport {

	public int doStartTag() throws JspTagException{
//		menuDatas = (List)pageContext.getRequest().getAttribute(this.getMenusName());
//		System.out.println("menuDatas---->"+menuDatas);
//		System.out.println("getId()---->"+getId());
		if(getId() == null || "".equals(getId()) || menuDatas == null || menuDatas.size() == 0){
			return SKIP_BODY;
		}

		this.count = 0;

		StringBuffer out = new StringBuffer();
		if(popupMenu == true){
			out.append("<div id=\""+getId()+"_div\" class=\""+this.getPopupClass()+"\" onblur=\"hideThird('"+getId()+"_div')\" >");
			out.append("<iframe src=\"javascript:false\" style=\"position:absolute; visibility:inherit; top:0px; left:0px; width:100%;height:100%; z-index:-1; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';\"></iframe>");
		}

		if(display == true){
			out.append("<table border=\"0\" class=\""+this.getClassName()+"\" cellpadding=\"0\" cellspacing=\"0\" id=\""+this.getId()+"\">");
		}else{
			out.append("<table border=\"0\" class=\""+this.getClassName()+"\" style=\"display:none\" cellpadding=\"0\" cellspacing=\"0\" id=\""+this.getId()+"\">");
		}
		if(popupMenu == false){
			out.append("<tr>");
		}
		if(menuDatas == null){
			menuDatas = new ArrayList();
		}
		if(menuDatas.size() > 0){

			pageContext.getRequest().setAttribute(this.getVar(), menuDatas.get(count));
			count++;
		}
		try {

			pageContext.getOut().write(out.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return EVAL_BODY_INCLUDE;


	}

	public int doAfterBody() throws JspException{

		if(count < menuDatas.size()){
			pageContext.getRequest().setAttribute(this.getVar(), menuDatas.get(count));

			count++;
			return EVAL_BODY_BUFFERED;
		}else{
			return SKIP_BODY;
		}
	}


	public void setBodyContent(BodyContent b)
    {
		System.out.println("in setBodyContent");
        bodyContent = b;
    }

	public int doEndTag() throws JspTagException{
		if(getId() == null || "".equals(getId()) || menuDatas == null || menuDatas.size() == 0){
			return EVAL_PAGE;
		}
		StringBuffer out = new StringBuffer();
		if(popupMenu == false){
			out.append("</tr>");
		}
		out.append("</table>");
		if(popupMenu == true){
			out.append("</div>");
		}
		try {
			pageContext.getOut().write(out.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;



	}


	private String id;//table标签的id
	private String var;//遍历变量
	private Integer count;//遍历item的次数
	private List menuDatas;//菜单数据集合
	private String level;//菜单级数
	private String className;//table样式
	private Boolean display = true;//是否显示
	private Boolean popupMenu = false;//是否弹出菜单
	private String popupClass;//弹出菜单样式

//	private String menuListName;

	public String getPopupClass() {
		return popupClass;
	}

	public void setPopupClass(String popupClass) {
		this.popupClass = popupClass;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getVar() {
		return var;
	}


	public void setVar(String var) {
		this.var = var;
	}



	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public List getMenuDatas() {
		return menuDatas;
	}

	public void setMenuDatas(List menuDatas) {
		this.menuDatas = menuDatas;
	}

	public Boolean getDisplay() {
		return display;
	}

	public void setDisplay(Boolean display) {
		this.display = display;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Boolean getPopupMenu() {
		return popupMenu;
	}

	public void setPopupMenu(Boolean popupMenu) {
		this.popupMenu = popupMenu;
	}


	public void release(){
//		System.out.println("in release()");
        super.release();
        this.id = null;
    	this.var = null;
    	this.count = null;
    	this.menuDatas = null;
    	this.level = null;
    	this.className = null;
    	this.display = null;
    	this.popupMenu = null;
    	this.popupClass = null;
    }

}
